/*-------------------------------------------------------------------------------
* file:     app_sleeptimer.c
* brief:    application
* copyright Copyright (C) 2020-2022 @ NanoIC
* Date:     2022-08-15
* Version:  V1.00
* Author:   JMH
*-------------------------------------------------------------------------------*/

/* include ----------------------------------------------------------------*/
#include "app_sleeptimer.h"

/* externs variablesr------------------------------------------------------*/
extern uint8_t one_wire_bus_app_sleep_or_wakeup_flag;

/* externs function--------------------------------------------------------*/

/* Private variablesr------------------------------------------------------*/
volatile uint16_t global_sleep_timer_100ms_tick; //sleeptimer时基
uint16_t global_keyboard_15_s_in_sleep_tick; //按键15秒休眠计时
uint16_t global_in_work_sleep_info_tick;      //进入15s休眠发包计时

volatile bool golbal_keyboard_15_s_in_sleep_flag;  //按键15秒休眠标志
volatile bool global_500ms_weakup_process_flag;
uint16_t global_sleep_15_s_in_sleep_tick=0;  //待机15秒休眠计时
volatile bool golbal_sleep_15_s_in_sleep_flag;  //待机休眠标志
/* Private function--------------------------------------------------------*/
/**
 * @brief ：定时器调用的低功耗相关函数,0.1mS调一次
 * 
 */
void app_sleep_timer_tick_0_1ms_callback(void)
{
    global_sleep_timer_100ms_tick ++;
     if(global_sleep_timer_100ms_tick >= 1000)
    {
        //100 ms
        global_sleep_timer_100ms_tick = 0x00;
        if(global_keyboard_15_s_in_sleep_tick < 150)
        {
            //15秒计时
            global_keyboard_15_s_in_sleep_tick ++;
            if(global_keyboard_15_s_in_sleep_tick == 149)
            {
                //到达15s计时
                    #if 0
                    printf("send sleep tick\n");
                    #endif
            }
        }else
        {
            //若15秒休眠请求没有回复则重复发送
            if(!golbal_keyboard_15_s_in_sleep_flag)
            {
                global_keyboard_15_s_in_sleep_tick = 140;
            }
        }
		
		if(global_sleep_15_s_in_sleep_tick < 300)
        {
            //15秒计时
            global_sleep_15_s_in_sleep_tick ++;
            if(global_sleep_15_s_in_sleep_tick == 299)
            {
                //到达15s计时
                    #if 0
                    printf("send sleep tick\n");
                    #endif
//                one_wire_bus_phy_adjust_tx_timer3(2);
//                one_wrie_bus_app_send_weakup_beat(0);
            }
        }else
        {
            //若15秒休眠请求没有回复则重复发送
            if(!golbal_sleep_15_s_in_sleep_flag)
            {
                golbal_sleep_15_s_in_sleep_flag = true;
			}
		}
    }

//    if(global_in_work_sleep_info_tick > 0)
//    {
//        //唤醒之后等待发心跳包之后进入休眠延时
//        global_in_work_sleep_info_tick --;
//        if(global_in_work_sleep_info_tick == 0)
//        {
//            //设置休眠标志
//            app_lowpower_set_in_sleep_flag();
//        }
//    }
}

void app_sleep_timer_15s_tick_reset(void)
{
    global_keyboard_15_s_in_sleep_tick = 0;
}

/**
 * @brief 设置进入15休眠之后，发包到休眠之间的时间控制
 */
void app_sleep_timer_set(void)
{
    global_in_work_sleep_info_tick = 26;
}

/**
 * @brief 
 */
void app_sleep_timer_500ms_process(void)
{
    if(global_500ms_weakup_process_flag)
    {
        global_500ms_weakup_process_flag = false;
//        one_wire_bus_app_tp_updata_timer_tick(0);
//        one_wrie_bus_app_send_heart_beat();
//        one_wire_bus_phy_adjust_tx_timer3(2);
        one_wire_bus_app_sleep_or_wakeup_flag = 0x01;
        app_sleep_timer_set();
    }
}

/**
 * @brief 设置15s休眠状态设置
 */
void app_lowpower_15s_in_status_set(void)
{
    golbal_keyboard_15_s_in_sleep_flag = true;
    one_wire_bus_app_sleep_or_wakeup_flag = 1;
    app_sleep_timer_set();
    #if 0
    printf("set sleep\n");
    #endif
}

/**
 * @brief 初始化
 */
void app_sleep_timer_init(void)
{
    #if 0
    printf("sleeptimer init\n");
    #endif
    global_sleep_timer_100ms_tick = 0x00;
    global_keyboard_15_s_in_sleep_tick = 0x00;
    global_in_work_sleep_info_tick = 0x00;
    golbal_keyboard_15_s_in_sleep_flag = false;
}

//---------------------------------------------------------------
void LPT_Start(void)
{
    LPT->RSSR |= 0X01;
}

void LPT_Stop(void)
{
    LPT->RSSR &= 0XFFFFFFFE;
}

void LPT_INT_ENABLE(void)
{
    INTC_ISER_WRITE(LPT_INT);
}

void LPT_INT_DISABLE(void)
{
    INTC_ICER_WRITE(LPT_INT);
}

void LPT_ConfigInterrupt_CMD(bool NewState,LPT_IMSCR_TypeDef LPT_IMSCR_X)
{
    if (NewState != false)
    {
        LPT->IMCR  |= LPT_IMSCR_X;						
    }
    else
    {
        LPT->IMCR  &= ~LPT_IMSCR_X;					
    }
}

/**
 * @brief 配置低功耗定时器
 * 
 */
void Lpt_Config(void)
{
    // LPT_DeInit();
    LPT->CEDR 		= 0xBE980000;
    LPT->RSSR 		= LPT_RESET_VALUE;
    LPT->PSCR 		= LPT_RESET_VALUE;
    LPT->CR	  		= 0X00010010;
    LPT->SYNCR 		= LPT_RESET_VALUE;
    LPT->PRDR 		= LPT_RESET_VALUE;
    LPT->CMP 		= LPT_RESET_VALUE; 
    LPT->CNT 		= LPT_RESET_VALUE;
    LPT->TRGFCR 	= LPT_RESET_VALUE;
    LPT->TRGFWR 	= LPT_RESET_VALUE;
    LPT->EVTRG 		= LPT_RESET_VALUE;
    LPT->EVPS		= LPT_RESET_VALUE;
    LPT->EVSWF 		= LPT_RESET_VALUE;
    // LPT->RISR 		= LPT_RESET_VALUE;
    // LPT->MISR 		= LPT_RESET_VALUE;
    LPT->IMCR 		= LPT_RESET_VALUE;
    LPT->ICR 		= LPT_RESET_VALUE;
    // LPT_Configure(LPTCLK_EN,LPT_ISCLK,LPT_IMMEDIATE,LPT_PSC_DIV4,0,LPT_OPM_CONTINUOUS);//选择27K 4分频

    //选择27K分频
    LPT->CEDR |=1| 4| 64| (0<<8);
    LPT->PSCR = 4;                              
    LPT->CR |=0;

    // LPT_Period_CMP_Write(27000,0);               //周期4s一次中断，时钟源27K，定时时间 = 27K/4*27000
    LPT->PRDR = 840;//180ms - 310 250ms- 450
    LPT->CMP = 839;
    LPT_ConfigInterrupt_CMD(true,LPT_MATCH);      //使能周期中断
    LPT_INT_ENABLE();                           //使能LPT中断

    LPT_Start();

    SYSCON->WKCR |= 1<<10;     //LPT_WKEN
    SYSCON->GCER |= 1<<12;     //使能Deepsleep模式下ISCLK继续工作
    INTC_IWER_WRITE(LPT_INT);  //使能LPT唤醒 
}




//---------------------------------------------------------------




















